% eye_curve_analysis
clear
close all
cd 'C:\Data\data_fMRI\_C_data'
cd 'C:\Data\data_eyelink2\study_2\_C_DATA'
files = dir('*C.mat'); % find all files with the C.mat extention

for fp_num=1:length(files)
    file_name=files(fp_num).name
    load(file_name);
    if ~isfield(C,'error')
        C=london_quad_plot105(file_name)
        save(lower([C.name 'C.mat']), 'C');
        disp([' saved file: ' C.name 'C.mat']);
        
    end
    C.sac_area(length(C.error))=0;
    for tr_num=find(C.error==0) ;       
        subplot(2,1,1)
        hold on;         
        %         plot(C.time(C.sac_on_loc(tr_num)-20:C.sac_off_loc(tr_num)+20)-C.fix_on(tr_num),...
        %             ((C.speed(C.sac_on_loc(tr_num)-20:C.sac_off_loc(tr_num)+20))/25)-15,'.-g')
        %         axis tight
        %         hline(0); 
        plot(C.time(C.sac_on_loc(tr_num)-10:C.sac_off_loc(tr_num)+10)-C.time(C.sac_on_loc(tr_num)),...
            (C.eye_x(C.sac_on_loc(tr_num)-10:C.sac_off_loc(tr_num)+10))-C.x_adj(tr_num),'.-b');
        plot(C.time(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.time(C.sac_on_loc(tr_num)),...
            (C.eye_x(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num)))-C.x_adj(tr_num),'.-c')
        plot(C.time(C.sac_on_loc(tr_num)-10:C.sac_off_loc(tr_num)+10)-C.time(C.sac_on_loc(tr_num)),...
            (C.eye_y(C.sac_on_loc(tr_num)-10:C.sac_off_loc(tr_num)+10))-C.y_adj(tr_num),'.-r');
        plot(C.time(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.time(C.sac_on_loc(tr_num)),...
            (C.eye_y(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num)))-C.y_adj(tr_num),'.-m')
        ylim([-15 15 ]);
        
        subplot(2,2,4);
        hold on;
        plot(C.eye_x(C.sac_on_loc(tr_num)-10:C.sac_off_loc(tr_num)+10)-C.eye_x(C.sac_on_loc(tr_num)),...
            C.eye_y(C.sac_on_loc(tr_num)-10:C.sac_off_loc(tr_num)+10)-C.eye_y(C.sac_on_loc(tr_num)),'-k');
        if C.anti(tr_num)==1
            plot(C.eye_x(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.eye_x(C.sac_on_loc(tr_num)),...
                C.eye_y(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.eye_y(C.sac_on_loc(tr_num)),'-r');
        else
            plot(C.eye_x(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.eye_x(C.sac_on_loc(tr_num)),...
                C.eye_y(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.eye_y(C.sac_on_loc(tr_num)),'-b');
        end
        axis([-15 15 -15 15]);axis square;
        
        subplot(2,2,3);
        hold on;
        %         plot(C.eye_x(C.sac_on_loc(tr_num)-10:C.sac_off_loc(tr_num)+10)-C.eye_x(C.sac_on_loc(tr_num)),...
        %             C.eye_y(C.sac_on_loc(tr_num)-10:C.sac_off_loc(tr_num)+10)-C.eye_y(C.sac_on_loc(tr_num)),'-k');
        %         if C.anti(tr_num)==1
        %             plot(C.eye_x(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.eye_x(C.sac_on_loc(tr_num)),...
        %                 C.eye_y(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.eye_y(C.sac_on_loc(tr_num)),'-r');
        %         else
        %             plot(C.eye_x(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.eye_x(C.sac_on_loc(tr_num)),...
        %                 C.eye_y(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.eye_y(C.sac_on_loc(tr_num)),'-b');
        %         end
        axis([-15 15 -15 15]);axis square;
        
        
        x=C.eye_x(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.x_adj(tr_num);
        y=C.eye_y(C.sac_on_loc(tr_num):C.sac_off_loc(tr_num))-C.x_adj(tr_num);
        [x2, y2]=rotate_saccade(x,y);
        C.sac_area(tr_num)=polyarea(x,y);
        subplot(2,2,3);
        area(x2,y2);
        if C.anti(tr_num)==1
            plot(x2,y2,'r');
        else
            plot(x2,y2,'b');
        end
        
        
    end
    subplot('position',[.46 .1 .1 .2] );cla;hold on;
    anti_mean_area=mean(C.sac_area(C.anti==1 & C.error'==0));
    anti_ste_area=ste(C.sac_area(C.anti==1 & C.error'==0));
    pro_mean_area=mean(C.sac_area(C.anti==0 & C.error'==0));
    pro_ste_area=ste(C.sac_area(C.anti==0 & C.error'==0));  
    bar(1,pro_mean_area,'b');
    bar(2,anti_mean_area,'r');
    ERRORBAR(1,pro_mean_area,pro_ste_area,'c');
    ERRORBAR(2,anti_mean_area,anti_ste_area,'m');
    [P]=ranksum(C.sac_area(C.anti==1 & C.error'==0),C.sac_area(C.anti==0 & C.error'==0));
    text(1,10,num2str(round(P*1000)/1000));
    axis tight;ylim([0 20]);   
    pause
    D=C;
    save(lower([D.name 'D.mat']), 'D');
    disp([' saved file: ' D.name 'D.mat']);
    clf;
end




return

% eye_curve_analysis
clear
close all
%cd 'C:\Data\data_eyelink2\study_1\_C_DATA'
files = dir('*D.mat'); % find all files with the C.mat extention
subject=[];
subject_nb=[];
for fp_num=1:length(files)
    file_name=files(fp_num).name
    load(file_name);
    subject=[subject; D.name(1:2) ];
    if subject_nb
        if (D.name(1)==subject(fp_num-1,1))*(D.name(2)==subject(fp_num-1,2))% curent subject == last subject  
            subject_nb=[subject_nb; subject_nb(end) ];
        else
            subject_nb=[subject_nb; subject_nb(end)+1 ];
        end
    else 
        subject_nb=1;
    end
    
    anti_mean_area(fp_num)=mean(D.sac_area(D.anti==1 & D.error'==0));
    anti_ste_area(fp_num)=ste(D.sac_area(D.anti==1 & D.error'==0));
    pro_mean_area(fp_num)=mean(D.sac_area(D.anti==0 & D.error'==0));
    pro_ste_area(fp_num)=ste(D.sac_area(D.anti==0 & D.error'==0));
    
    subplot(ceil(sqrt(length(files))),ceil(sqrt(length(files))),fp_num);
    cla;hold on;
    bar(1,pro_mean_area(fp_num),'b');
    bar(2,anti_mean_area(fp_num),'r');
    ERRORBAR(1,pro_mean_area(fp_num),pro_ste_area(fp_num),'c');
    ERRORBAR(2,anti_mean_area(fp_num),anti_ste_area(fp_num),'m');
    [P]=ranksum(D.sac_area(D.anti==1 & D.error'==0),D.sac_area(D.anti==0 & D.error'==0));
    text(1,10,num2str(round(P*1000)/1000));
    axis tight;ylim([0 20]);    
end


for sub=unique(subject_nb)'
    anti_sub_mean(sub)=mean(anti_mean_area(subject_nb==sub));
     pro_sub_mean(sub)=mean( pro_mean_area(subject_nb==sub));
end
anti_sub_mean'
pro_sub_mean'
subject(find(diff(subject_nb)),:)